package com.sanbi.erp.core.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日期格式化工具类
 */
public class DateTimeUtil {

    private static final Log logger = LogFactory.getLog(DateTimeUtil.class);

    /**
     * 格式 yyyy年MM月dd日 HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String getDateTimeDisplayString(LocalDateTime dateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        return dtf2.format(dateTime);
    }

    /**
     * 格式 yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static LocalDateTime getDisplayLocalDateTime(String date) {
        LocalDateTime parseLocalDate = null;
        try {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            parseLocalDate = LocalDateTime.parse(date, dtf);
        } catch (Exception e) {
            //格式有误时抛出
            logger.error("时间格式错误", e);
        }
        return parseLocalDate;
    }

    public static void main(String[] args) {
        String str = "2022-08-02 00:00:";
        LocalDateTime b;
        b = getDisplayLocalDateTime(str);
        System.out.println("结果：" + b);
    }
}
